home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 9 / AMUG BBS in a Box Volume IX (August 1993) (MacWizards).iso / Files / Prog / U-Z / zmodem.source.cpt / sz.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-15  |  37.0 KB  |  1,711 lines

  1. #define VERSION "sz 2.10 05-09-88"
  2. #define PUBDIR "/usr/spool/uucppublic"
  3.  
  4. /*% cc -compat -M2 -Ox -K -i -DTXBSIZE=16384  -DNFGVMIN -DREADCHECK sz.c -lx -o sz; size sz
  5.  
  6.     Following is used for testing, might not be reasonable for production
  7. <-xtx-*> cc -Osal -DTXBSIZE=32768  -DSV sz.c -lx -o $B/sz; size $B/sz
  8.  
  9.  ****************************************************************************
  10.  *
  11.  * sz.c By Chuck Forsberg,  Omen Technology INC
  12.  *
  13.  ****************************************************************************
  14.  *
  15.  * Typical Unix/Xenix/Clone compiles:
  16.  *
  17.  *    cc -O sz.c -o sz        USG (SYS III/V) Unix
  18.  *    cc -O -DSV sz.c -o sz        Sys V Release 2 with non-blocking input
  19.  *                    Define to allow reverse channel checking
  20.  *    cc -O -DV7  sz.c -o sz        Unix Version 7, 2.8 - 4.3 BSD
  21.  *
  22.  *    cc -O -K -i -DNFGVMIN -DREADCHECK sz.c -lx -o sz    Classic Xenix
  23.  *
  24.  *    ln sz sb            **** All versions ****
  25.  *    ln sz sx            **** All versions ****
  26.  *
  27.  ****************************************************************************
  28.  *
  29.  * Typical VMS compile and install sequence:
  30.  *
  31.  *        define LNK$LIBRARY   SYS$LIBRARY:VAXCRTL.OLB
  32.  *        cc sz.c
  33.  *        cc vvmodem.c
  34.  *        link sz,vvmodem
  35.  *    sz :== $disk$user2:[username.subdir]sz.exe
  36.  *
  37.  *  If you feel adventureous, remove the #define BADSYNC line
  38.  *  immediately following the #ifdef vax11c line!  Some VMS
  39.  *  systems know how to fseek, some don't.
  40.  *
  41.  ****************************************************************************
  42.  *
  43.  *
  44.  * A program for Unix to send files and commands to computers running
  45.  *  Professional-YAM, PowerCom, YAM, IMP, or programs supporting Y/XMODEM.
  46.  *
  47.  *  Sz uses buffered I/O to greatly reduce CPU time compared to UMODEM.
  48.  *
  49.  *  USG UNIX (3.0) ioctl conventions courtesy Jeff Martin
  50.  *
  51.  *  2.1x hacks to avoid VMS fseek() bogosity, allow streaming if input from pipe
  52.  *     -DBADSEEK -DTXBSIZE=32768  
  53.  *  2.x has mods for VMS flavor
  54.  *
  55.  * 1.34 implements tx backchannel garbage count and ZCRCW after ZRPOS
  56.  * in accordance with the 7-31-87 ZMODEM Protocol Description
  57.  */
  58.  
  59.  
  60. char *substr(), *getenv();
  61.  
  62. #ifdef vax11c
  63. #define BADSEEK
  64. #define TXBSIZE 32768        /* Must be power of two, < MAXINT */
  65. #include <types.h>
  66. #include <stat.h>
  67. #define LOGFILE "szlog.tmp"
  68. #include <stdio.h>
  69. #include <signal.h>
  70. #include <setjmp.h>
  71. #include <ctype.h>
  72. #include <errno.h>
  73. #define OS "VMS"
  74. #define READCHECK
  75. #define BUFWRITE
  76. #define iofd
  77. extern int errno;
  78. #define SS_NORMAL SS$_NORMAL
  79. #define xsendline(c) sendline(c)
  80.  
  81.  
  82. #else    /* vax11c */
  83.  
  84.  
  85. #define SS_NORMAL 0
  86. #define LOGFILE "/tmp/szlog"
  87. #include <stdio.h>
  88. #include <signal.h>
  89. #include <setjmp.h>
  90. #include <ctype.h>
  91. #include <errno.h>
  92. extern int errno;
  93.  
  94. #define sendline(c) putchar(c & 0377)
  95. #define xsendline(c) putchar(c)
  96.  
  97. #endif
  98.  
  99. #define PATHLEN 256
  100. #define OK 0
  101. #define FALSE 0
  102. #define TRUE 1
  103. #define ERROR (-1)
  104. /* Ward Christensen / CP/M parameters - Don't change these! */
  105. #define ENQ 005
  106. #define CAN ('X'&037)
  107. #define XOFF ('s'&037)
  108. #define XON ('q'&037)
  109. #define SOH 1
  110. #define STX 2
  111. #define EOT 4
  112. #define ACK 6
  113. #define NAK 025
  114. #define CPMEOF 032
  115. #define WANTCRC 0103    /* send C not NAK to get crc not checksum */
  116. #define WANTG 0107    /* Send G not NAK to get nonstop batch xmsn */
  117. #define TIMEOUT (-2)
  118. #define RCDO (-3)
  119. #define RETRYMAX 10
  120.  
  121.  
  122. #define HOWMANY 2
  123. int Zmodem=0;        /* ZMODEM protocol requested by receiver */
  124. unsigned Baudrate=2400;    /* Default, should be set by first mode() call */
  125. unsigned Txwindow;    /* Control the size of the transmitted window */
  126. unsigned Txwspac;    /* Spacing between zcrcq requests */
  127. unsigned Txwcnt;    /* Counter used to space ack requests */
  128. long Lrxpos;        /* Receiver's last reported offset */
  129. int errors;
  130.  
  131. #ifdef vax11c
  132. #include "vrzsz.c"    /* most of the system dependent stuff here */
  133. #else
  134. #include "rbsb.c"    /* most of the system dependent stuff here */
  135. #endif
  136. #include "crctab.c"
  137.  
  138. int Filesleft;
  139. long Totalleft;
  140.  
  141. /*
  142.  * Attention string to be executed by receiver to interrupt streaming data
  143.  *  when an error is detected.  A pause (0336) may be needed before the
  144.  *  ^C (03) or after it.
  145.  */
  146. #ifdef READCHECK
  147. char Myattn[] = { 0 };
  148. #else
  149. #ifdef USG
  150. char Myattn[] = { 03, 0336, 0 };
  151. #else
  152. char Myattn[] = { 0 };
  153. #endif
  154. #endif
  155.  
  156. FILE *in;
  157.  
  158. #ifdef BADSEEK
  159. int Canseek = 0;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  160. #ifndef TXBSIZE
  161. #define TXBSIZE 16384        /* Must be power of two, < MAXINT */
  162. #endif
  163. #else
  164. int Canseek = 1;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  165. #endif
  166.  
  167. #ifdef TXBSIZE
  168. #define TXBMASK (TXBSIZE-1)
  169. char Txb[TXBSIZE];        /* Circular buffer for file reads */
  170. char *txbuf = Txb;        /* Pointer to current file segment */
  171. #else
  172. char txbuf[1024];
  173. #endif
  174. long vpos = 0;            /* Number of bytes read from file */
  175.  
  176. char Lastrx;
  177. char Crcflg;
  178. int Verbose=0;
  179. int Modem2=0;        /* XMODEM Protocol - don't send pathnames */
  180. int Restricted=0;    /* restricted; no /.. or ../ in filenames */
  181. int Quiet=0;        /* overrides logic that would otherwise set verbose */
  182. int Ascii=0;        /* Add CR's for brain damaged programs */
  183. int Fullname=0;        /* transmit full pathname */
  184. int Unlinkafter=0;    /* Unlink file after it is sent */
  185. int Dottoslash=0;    /* Change foo.bar.baz to foo/bar/baz */
  186. int firstsec;
  187. int errcnt=0;        /* number of files unreadable */
  188. int blklen=128;        /* length of transmitted records */
  189. int Optiong;        /* Let it rip no wait for sector ACK's */
  190. int Eofseen;        /* EOF seen on input set by zfilbuf */
  191. int BEofseen;        /* EOF seen on input set by fooseek */
  192. int Totsecs;        /* total number of sectors this file */
  193. int Filcnt=0;        /* count of number of files opened */
  194. int Lfseen=0;
  195. unsigned Rxbuflen = 16384;    /* Receiver's max buffer length */
  196. int Tframlen = 0;    /* Override for tx frame length */
  197. int blkopt=0;        /* Override value for zmodem blklen */
  198. int Rxflags = 0;
  199. long bytcnt;
  200. int Wantfcs32 = TRUE;    /* want to send 32 bit FCS */
  201. char Lzconv;    /* Local ZMODEM file conversion request */
  202. char Lzmanag;    /* Local ZMODEM file management request */
  203. int Lskipnocor;
  204. char Lztrans;
  205. char zconv;        /* ZMODEM file conversion request */
  206. char zmanag;        /* ZMODEM file management request */
  207. char ztrans;        /* ZMODEM file transport request */
  208. int Command;        /* Send a command, then exit. */
  209. char *Cmdstr;        /* Pointer to the command string */
  210. int Cmdtries = 11;
  211. int Cmdack1;        /* Rx ACKs command, then do it */
  212. int Exitcode;
  213. int Test;        /* 1= Force receiver to send Attn, etc with qbf. */
  214.             /* 2= Character transparency test */
  215. char *qbf="The quick brown fox jumped over the lazy dog's back 1234567890\r\n";
  216. long Lastread;        /* Beginning offset of last buffer read */
  217. int Lastn;        /* Count of last buffer read or -1 */
  218. int Dontread;        /* Don't read the buffer, it's still there */
  219. long Lastsync;        /* Last offset to which we got a ZRPOS */
  220. int Beenhereb4;        /* How many times we've been ZRPOS'd same place */
  221.  
  222. jmp_buf tohere;        /* For the interrupt on RX timeout */
  223. jmp_buf intrjmp;    /* For the interrupt on RX CAN */
  224.  
  225. /* called by signal interrupt or terminate to clean things up */
  226. bibi(n)
  227. {
  228.     canit(); fflush(stdout); mode(0);
  229.     fprintf(stderr, "sz: caught signal %d; exiting\n", n);
  230.     if (n == SIGQUIT)
  231.         abort();
  232.     if (n == 99)
  233.         fprintf(stderr, "mode(2) in rbsb.c not implemented!!\n");
  234.     cucheck();
  235.     exit(128+n);
  236. }
  237. /* Called when ZMODEM gets an interrupt (^X) */
  238. onintr()
  239. {
  240.     signal(SIGINT, SIG_IGN);
  241.     longjmp(intrjmp, -1);
  242. }
  243.  
  244. int Zctlesc;    /* Encode control characters */
  245. int Nozmodem = 0;    /* If invoked as "sb" */
  246. char *Progname = "sz";
  247. int Zrwindow = 1400;    /* RX window size (controls garbage count) */
  248. #include "zm.c"
  249.  
  250.  
  251. main(argc, argv)
  252. char *argv[];
  253. {
  254.     register char *cp;
  255.     register npats;
  256.     int dm;
  257.     char **patts;
  258.     static char xXbuf[BUFSIZ];
  259.  
  260.     if ((cp = getenv("ZNULLS")) && *cp)
  261.         Znulls = atoi(cp);
  262.     if ((cp=getenv("SHELL")) && (substr(cp, "rsh") || substr(cp, "rksh")))
  263.         Restricted=TRUE;
  264.     from_cu();
  265.     chkinvok(argv[0]);
  266.  
  267.     Rxtimeout = 600;
  268.     npats=0;
  269.     if (argc<2)
  270.         usage();
  271.     setbuf(stdout, xXbuf);        
  272.     while (--argc) {
  273.         cp = *++argv;
  274.         if (*cp++ == '-' && *cp) {
  275.             while ( *cp) {
  276.                 switch(*cp++) {
  277.                 case '\\':
  278.                      *cp = toupper(*cp);  continue;
  279.                 case '+':
  280.                     Lzmanag = ZMAPND; break;
  281. #ifdef CSTOPB
  282.                 case '2':
  283.                     Twostop = TRUE; break;
  284. #endif
  285.                 case 'a':
  286.                     Lzconv = ZCNL;
  287.                     Ascii = TRUE; break;
  288.                 case 'b':
  289.                     Lzconv = ZCBIN; break;
  290.                 case 'C':
  291.                     if (--argc < 1) {
  292.                         usage();
  293.                     }
  294.                     Cmdtries = atoi(*++argv);
  295.                     break;
  296.                 case 'i':
  297.                     Cmdack1 = ZCACK1;
  298.                     /* **** FALL THROUGH TO **** */
  299.                 case 'c':
  300.                     if (--argc != 1) {
  301.                         usage();
  302.                     }
  303.                     Command = TRUE;
  304.                     Cmdstr = *++argv;
  305.                     break;
  306.                 case 'd':
  307.                     ++Dottoslash;
  308.                     /* **** FALL THROUGH TO **** */
  309.                 case 'f':
  310.                     Fullname=TRUE; break;
  311.                 case 'e':
  312.                     Zctlesc = 1; break;
  313.                 case 'k':
  314.                     blklen=1024; break;
  315.                 case 'L':
  316.                     if (--argc < 1) {
  317.                         usage();
  318.                     }
  319.                     blkopt = atoi(*++argv);
  320.                     if (blkopt<24 || blkopt>1024)
  321.                         usage();
  322.                     break;
  323.                 case 'l':
  324.                     if (--argc < 1) {
  325.                         usage();
  326.                     }
  327.                     Tframlen = atoi(*++argv);
  328.                     if (Tframlen<32 || Tframlen>1024)
  329.                         usage();
  330.                     break;
  331.                 case 'N':
  332.                     Lzmanag = ZMNEWL;  break;
  333.                 case 'n':
  334.                     Lzmanag = ZMNEW;  break;
  335.                 case 'o':
  336.                     Wantfcs32 = FALSE; break;
  337.                 case 'p':
  338.                     Lzmanag = ZMPROT;  break;
  339.                 case 'r':
  340.                     Lzconv = ZCRESUM;
  341.                 case 'q':
  342.                     Quiet=TRUE; Verbose=0; break;
  343.                 case 't':
  344.                     if (--argc < 1) {
  345.                         usage();
  346.                     }
  347.                     Rxtimeout = atoi(*++argv);
  348.                     if (Rxtimeout<10 || Rxtimeout>1000)
  349.                         usage();
  350.                     break;
  351.                 case 'T':
  352.                     if (++Test > 1) {
  353.                         chartest(1); chartest(2);
  354.                         mode(0);  exit(0);
  355.                     }
  356.                     break;
  357. #ifndef vax11c
  358.                 case 'u':
  359.                     ++Unlinkafter; break;
  360. #endif
  361.                 case 'v':
  362.                     ++Verbose; break;
  363.                 case 'w':
  364.                     if (--argc < 1) {
  365.                         usage();
  366.                     }
  367.                     Txwindow = atoi(*++argv);
  368.                     if (Txwindow < 256)
  369.                         Txwindow = 256;
  370.                     Txwindow = (Txwindow/64) * 64;
  371.                     Txwspac = Txwindow/4;
  372.                     if (blkopt > Txwspac
  373.                      || (!blkopt && Txwspac < 1024))
  374.                         blkopt = Txwspac;
  375.                     break;
  376.                 case 'X':
  377.                     ++Modem2; break;
  378.                 case 'Y':
  379.                     Lskipnocor = TRUE;
  380.                     /* **** FALLL THROUGH TO **** */
  381.                 case 'y':
  382.                     Lzmanag = ZMCLOB; break;
  383.                 default:
  384.                     usage();
  385.                 }
  386.             }
  387.         }
  388.         else if ( !npats && argc>0) {
  389.             if (argv[0][0]) {
  390.                 npats=argc;
  391.                 patts=argv;
  392. #ifndef vax11c
  393.                 if ( !strcmp(*patts, "-"))
  394.                     iofd = 1;
  395. #endif
  396.             }
  397.         }
  398.     }
  399.     if (npats < 1 && !Command && !Test) 
  400.         usage();
  401.     if (Verbose) {
  402.         if (freopen(LOGFILE, "a", stderr)==NULL) {
  403.             printf("Can't open log file %s\n",LOGFILE);
  404.             exit(0200);
  405.         }
  406.         setbuf(stderr, NULL);
  407.     }
  408.     if (Fromcu && !Quiet) {
  409.         if (Verbose == 0)
  410.             Verbose = 2;
  411.     }
  412.  
  413.     mode(1);
  414.  
  415.     if (signal(SIGINT, bibi) == SIG_IGN) {
  416.         signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN);
  417.     } else {
  418.         signal(SIGINT, bibi); signal(SIGKILL, bibi);
  419.     }
  420.     if ( !Fromcu)
  421.         signal(SIGQUIT, SIG_IGN);
  422.     signal(SIGTERM, bibi);
  423.  
  424.     if ( !Modem2) {
  425.         if (!Nozmodem) {
  426.             printf("rz\r");  fflush(stdout);
  427.         }
  428.         countem(npats, patts);
  429.         if (!Nozmodem) {
  430.             stohdr(0L);
  431.             if (Command)
  432.                 Txhdr[ZF0] = ZCOMMAND;
  433.             zshhdr(ZRQINIT, Txhdr);
  434.         }
  435.     }
  436.     fflush(stdout);
  437.  
  438.     if (Command) {
  439.         if (getzrxinit()) {
  440.             Exitcode=0200; canit();
  441.         }
  442.         else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  443.             Exitcode=0200; canit();
  444.         }
  445.     } else if (wcsend(npats, patts)==ERROR) {
  446.         Exitcode=0200;
  447.         canit();
  448.     }
  449.     fflush(stdout);
  450.     mode(0);
  451.     dm = ((errcnt != 0) | Exitcode);
  452.     if (dm) {
  453.         cucheck();  exit(dm);
  454.     }
  455.     exit(SS_NORMAL);
  456.     /*NOTREACHED*/
  457. }
  458.  
  459. wcsend(argc, argp)
  460. char *argp[];
  461. {
  462.     register n;
  463.  
  464.     Crcflg=FALSE;
  465.     firstsec=TRUE;
  466.     bytcnt = -1;
  467.     for (n=0; n<argc; ++n) {
  468.         Totsecs = 0;
  469.         if (wcs(argp[n])==ERROR)
  470.             return ERROR;
  471.     }
  472.     Totsecs = 0;
  473.     if (Filcnt==0) {    /* bitch if we couldn't open ANY files */
  474.         if ( !Modem2) {
  475.             Command = TRUE;
  476.             Cmdstr = "echo \"sz: Can't open any requested files\"";
  477.             if (getnak()) {
  478.                 Exitcode=0200; canit();
  479.             }
  480.             if (!Zmodem)
  481.                 canit();
  482.             else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  483.                 Exitcode=0200; canit();
  484.             }
  485.             Exitcode = 1; return OK;
  486.         }
  487.         canit();
  488.         fprintf(stderr,"\r\nCan't open any requested files.\r\n");
  489.         return ERROR;
  490.     }
  491.     if (Zmodem)
  492.         saybibi();
  493.     else if ( !Modem2)
  494.         wctxpn("");
  495.     return OK;
  496. }
  497.  
  498. wcs(oname)
  499. char *oname;
  500. {
  501.     register c;
  502.     register char *p;
  503.     struct stat f;
  504.     char name[PATHLEN];
  505.  
  506.     strcpy(name, oname);
  507.  
  508.     if (Restricted) {
  509.         /* restrict pathnames to current tree or uucppublic */
  510.         if ( substr(name, "../")
  511.          || (name[0]== '/' && strncmp(name, PUBDIR, strlen(PUBDIR))) ) {
  512.             canit();
  513.             fprintf(stderr,"\r\nsz:\tSecurity Violation\r\n");
  514.             return ERROR;
  515.         }
  516.     }
  517.  
  518.     if ( !strcmp(oname, "-")) {
  519.         if ((p = getenv("ONAME")) && *p)
  520.             strcpy(name, p);
  521.         else
  522.             sprintf(name, "s%d.sz", getpid());
  523.         in = stdin;
  524.     }
  525.     else if ((in=fopen(oname, "r"))==NULL) {
  526.         ++errcnt;
  527.         return OK;    /* pass over it, there may be others */
  528.     }
  529.     BEofseen = Eofseen = 0;  vpos = 0;
  530.     Lastread = 0;  Lastn = -1; Dontread = FALSE;
  531.     /* Check for directory or block special files */
  532.     fstat(fileno(in), &f);
  533.     c = f.st_mode & S_IFMT;
  534.     if (c == S_IFDIR || c == S_IFBLK) {
  535.         fclose(in);
  536.         return OK;
  537.     }
  538.  
  539.     ++Filcnt;
  540.     switch (wctxpn(name)) {
  541.     case ERROR:
  542.         return ERROR;
  543.     case ZSKIP:
  544.         return OK;
  545.     }
  546.     if (!Zmodem && wctx(f.st_size)==ERROR)
  547.         return ERROR;
  548. #ifndef vax11c
  549.     if (Unlinkafter)
  550.         unlink(oname);
  551. #endif
  552.     return 0;
  553. }
  554.  
  555. /*
  556.  * generate and transmit pathname block consisting of
  557.  *  pathname (null terminated),
  558.  *  file length, mode time and file mode in octal
  559.  *  as provided by the Unix fstat call.
  560.  *  N.B.: modifies the passed name, may extend it!
  561.  */
  562. wctxpn(name)
  563. char *name;
  564. {
  565.     register char *p, *q;
  566.     char name2[PATHLEN];
  567.     struct stat f;
  568.  
  569.     if (Modem2) {
  570.         if ((in!=stdin) && *name && fstat(fileno(in), &f)!= -1) {
  571.             fprintf(stderr, "Sending %s, %ld blocks: ",
  572.               name, f.st_size>>7);
  573.         }
  574.         fprintf(stderr, "Give your local XMODEM receive command now.\r\n");
  575.         return OK;
  576.     }
  577.     zperr("Awaiting pathname nak for %s", *name?name:"<END>");
  578.     if ( !Zmodem)
  579.         if (getnak())
  580.             return ERROR;
  581.  
  582.     q = (char *) 0;
  583.     if (Dottoslash) {        /* change . to . */
  584.         for (p=name; *p; ++p) {
  585.             if (*p == '/')
  586.                 q = p;
  587.             else if (*p == '.')
  588.                 *(q=p) = '/';
  589.         }
  590.         if (q && strlen(++q) > 8) {    /* If name>8 chars */
  591.             q += 8;            /*   make it .ext */
  592.             strcpy(name2, q);    /* save excess of name */
  593.             *q = '.';
  594.             strcpy(++q, name2);    /* add it back */
  595.         }
  596.     }
  597.  
  598.     for (p=name, q=txbuf ; *p; )
  599.         if ((*q++ = *p++) == '/' && !Fullname)
  600.             q = txbuf;
  601.     *q++ = 0;
  602.     p=q;
  603.     while (q < (txbuf + 1024))
  604.         *q++ = 0;
  605.     if (!Ascii && (in!=stdin) && *name && fstat(fileno(in), &f)!= -1)
  606.         sprintf(p, "%lu %lo %o 0 %d %ld", f.st_size, f.st_mtime,
  607.           f.st_mode, Filesleft, Totalleft);
  608.     Totalleft -= f.st_size;
  609.     if (--Filesleft <= 0)
  610.         Totalleft = 0;
  611.     if (Totalleft < 0)
  612.         Totalleft = 0;
  613.  
  614.     /* force 1k blocks if name won't fit in 128 byte block */
  615.     if (txbuf[125])
  616.         blklen=1024;
  617.     else {        /* A little goodie for IMP/KMD */
  618.         txbuf[127] = (f.st_size + 127) >>7;
  619.         txbuf[126] = (f.st_size + 127) >>15;
  620.     }
  621.     if (Zmodem)
  622.         return zsendfile(txbuf, 1+strlen(p)+(p-txbuf));
  623.     if (wcputsec(txbuf, 0, 128)==ERROR)
  624.         return ERROR;
  625.     return OK;
  626. }
  627.  
  628. getnak()
  629. {
  630.     register firstch;
  631.  
  632.     Lastrx = 0;
  633.     for (;;) {
  634.         switch (firstch = readline(800)) {
  635.         case ZPAD:
  636.             if (getzrxinit())
  637.                 return ERROR;
  638.             Ascii = 0;    /* Receiver does the conversion */
  639.             return FALSE;
  640.         case TIMEOUT:
  641.             zperr("Timeout on pathname");
  642.             return TRUE;
  643.         case WANTG:
  644. #ifdef MODE2OK
  645.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  646. #endif
  647.             Optiong = TRUE;
  648.             blklen=1024;
  649.         case WANTCRC:
  650.             Crcflg = TRUE;
  651.         case NAK:
  652.             return FALSE;
  653.         case CAN:
  654.             if ((firstch = readline(20)) == CAN && Lastrx == CAN)
  655.                 return TRUE;
  656.         default:
  657.             break;
  658.         }
  659.         Lastrx = firstch;
  660.     }
  661. }
  662.  
  663.  
  664. wctx(flen)
  665. long flen;
  666. {
  667.     register int thisblklen;
  668.     register int sectnum, attempts, firstch;
  669.     long charssent;
  670.  
  671.     charssent = 0;  firstsec=TRUE;  thisblklen = blklen;
  672.     vfile("wctx:file length=%ld", flen);
  673.  
  674.     while ((firstch=readline(Rxtimeout))!=NAK && firstch != WANTCRC
  675.       && firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)
  676.         ;
  677.     if (firstch==CAN) {
  678.         zperr("Receiver CANcelled");
  679.         return ERROR;
  680.     }
  681.     if (firstch==WANTCRC)
  682.         Crcflg=TRUE;
  683.     if (firstch==WANTG)
  684.         Crcflg=TRUE;
  685.     sectnum=0;
  686.     for (;;) {
  687.         if (flen <= (charssent + 896L))
  688.             thisblklen = 128;
  689.         if ( !filbuf(txbuf, thisblklen))
  690.             break;
  691.         if (wcputsec(txbuf, ++sectnum, thisblklen)==ERROR)
  692.             return ERROR;
  693.         charssent += thisblklen;
  694.     }
  695.     fclose(in);
  696.     attempts=0;
  697.     do {
  698.         purgeline();
  699.         sendline(EOT);
  700.         fflush(stdout);
  701.         ++attempts;
  702.     }
  703.         while ((firstch=(readline(Rxtimeout)) != ACK) && attempts < RETRYMAX);
  704.     if (attempts == RETRYMAX) {
  705.         zperr("No ACK on EOT");
  706.         return ERROR;
  707.     }
  708.     else
  709.         return OK;
  710. }
  711.  
  712. wcputsec(buf, sectnum, cseclen)
  713. char *buf;
  714. int sectnum;
  715. int cseclen;    /* data length of this sector to send */
  716. {
  717.     register checksum, wcj;
  718.     register char *cp;
  719.     unsigned oldcrc;
  720.     int firstch;
  721.     int attempts;
  722.  
  723.     firstch=0;    /* part of logic to detect CAN CAN */
  724.  
  725.     if (Verbose>2)
  726.         fprintf(stderr, "Sector %3d %2dk\n", Totsecs, Totsecs/8 );
  727.     else if (Verbose>1)
  728.         fprintf(stderr, "\rSector %3d %2dk ", Totsecs, Totsecs/8 );
  729.     for (attempts=0; attempts <= RETRYMAX; attempts++) {
  730.         Lastrx= firstch;
  731.         sendline(cseclen==1024?STX:SOH);
  732.         sendline(sectnum);
  733.         sendline(-sectnum -1);
  734.         oldcrc=checksum=0;
  735.         for (wcj=cseclen,cp=buf; --wcj>=0; ) {
  736.             sendline(*cp);
  737.             oldcrc=updcrc((0377& *cp), oldcrc);
  738.             checksum += *cp++;
  739.         }
  740.         if (Crcflg) {
  741.             oldcrc=updcrc(0,updcrc(0,oldcrc));
  742.             sendline((int)oldcrc>>8);
  743.             sendline((int)oldcrc);
  744.         }
  745.         else
  746.             sendline(checksum);
  747.  
  748.         if (Optiong) {
  749.             firstsec = FALSE; return OK;
  750.         }
  751.         firstch = readline(Rxtimeout);
  752. gotnak:
  753.         switch (firstch) {
  754.         case CAN:
  755.             if(Lastrx == CAN) {
  756. cancan:
  757.                 zperr("Cancelled");  return ERROR;
  758.             }
  759.             break;
  760.         case TIMEOUT:
  761.             zperr("Timeout on sector ACK"); continue;
  762.         case WANTCRC:
  763.             if (firstsec)
  764.                 Crcflg = TRUE;
  765.         case NAK:
  766.             zperr("NAK on sector"); continue;
  767.         case ACK: 
  768.             firstsec=FALSE;
  769.             Totsecs += (cseclen>>7);
  770.             return OK;
  771.         case ERROR:
  772.             zperr("Got burst for sector ACK"); break;
  773.         default:
  774.             zperr("Got %02x for sector ACK", firstch); break;
  775.         }
  776.         for (;;) {
  777.             Lastrx = firstch;
  778.             if ((firstch = readline(Rxtimeout)) == TIMEOUT)
  779.                 break;
  780.             if (firstch == NAK || firstch == WANTCRC)
  781.                 goto gotnak;
  782.             if (firstch == CAN && Lastrx == CAN)
  783.                 goto cancan;
  784.         }
  785.     }
  786.     zperr("Retry Count Exceeded");
  787.     return ERROR;
  788. }
  789.  
  790. /* fill buf with count chars padding with ^Z for CPM */
  791. filbuf(buf, count)
  792. register char *buf;
  793. {
  794.     register c, m;
  795.  
  796.     if ( !Ascii) {
  797.         m = read(fileno(in), buf, count);
  798.         if (m <= 0)
  799.             return 0;
  800.         while (m < count)
  801.             buf[m++] = 032;
  802.         return count;
  803.     }
  804.     m=count;
  805.     if (Lfseen) {
  806.         *buf++ = 012; --m; Lfseen = 0;
  807.     }
  808.     while ((c=getc(in))!=EOF) {
  809.         if (c == 012) {
  810.             *buf++ = 015;
  811.             if (--m == 0) {
  812.                 Lfseen = TRUE; break;
  813.             }
  814.         }
  815.         *buf++ =c;
  816.         if (--m == 0)
  817.             break;
  818.     }
  819.     if (m==count)
  820.         return 0;
  821.     else
  822.         while (--m>=0)
  823.             *buf++ = CPMEOF;
  824.     return count;
  825. }
  826.  
  827. /* Fill buffer with blklen chars */
  828. zfilbuf()
  829. {
  830.     int n;
  831.  
  832. #ifdef TXBSIZE
  833.     /* We assume request is within buffer, or just beyond */
  834.     txbuf = Txb + (bytcnt & TXBMASK);
  835.     if (vpos <= bytcnt) {
  836.         n = fread(txbuf, 1, blklen, in);
  837.         vpos += n;
  838.         if (n < blklen)
  839.             Eofseen = 1;
  840.         return n;
  841.     }
  842.     if (vpos >= (bytcnt+blklen))
  843.         return blklen;
  844.     /* May be a short block if crash recovery etc. */
  845.     Eofseen = BEofseen;
  846.     return (vpos - bytcnt);
  847. #else
  848.     n = fread(txbuf, 1, blklen, in);
  849.     if (n < blklen)
  850.         Eofseen = 1;
  851.     return n;
  852. #endif
  853. }
  854.  
  855. #ifdef TXBSIZE
  856. fooseek(fptr, pos, whence)
  857. FILE *fptr;
  858. long pos;
  859. {
  860.     int m, n;
  861.  
  862.     vfile("fooseek: pos =%lu vpos=%lu Canseek=%d", pos, vpos, Canseek);
  863.     /* Seek offset < current buffer */
  864.     if (pos < (vpos -TXBSIZE +1024)) {
  865.         BEofseen = 0;
  866.         if (Canseek > 0) {
  867.             vpos = pos & ~TXBMASK;
  868.             if (vpos >= pos)
  869.                 vpos -= TXBSIZE;
  870.             if (fseek(fptr, vpos, 0))
  871.                 return 1;
  872.         }
  873.         else if (Canseek == 0)
  874.             if (fseek(fptr, vpos = 0L, 0))
  875.                 return 1;
  876.         else
  877.             return 1;
  878.         while (vpos <= pos) {
  879.             n = fread(Txb, 1, TXBSIZE, fptr);
  880.             vpos += n;
  881.             vfile("n=%d vpos=%ld", n, vpos);
  882.             if (n < TXBSIZE) {
  883.                 BEofseen = 1;
  884.                 break;
  885.             }
  886.         }
  887.         vfile("vpos=%ld", vpos);
  888.         return 0;
  889.     }
  890.     /* Seek offset > current buffer (crash recovery, etc.) */
  891.     if (pos > vpos) {
  892.         if (Canseek)
  893.             if (fseek(fptr, vpos = (pos & ~TXBMASK), 0))
  894.                 return 1;
  895.         while (vpos <= pos) {
  896.             txbuf = Txb + (vpos & TXBMASK);
  897.             m = TXBSIZE - (vpos & TXBMASK);
  898.             n = fread(txbuf, 1, m, fptr);
  899.             vpos += n;
  900.             vfile("bo=%d n=%d vpos=%ld", txbuf-Txb, n, vpos);
  901.             if (m < n) {
  902.                 BEofseen = 1;
  903.                 break;
  904.             }
  905.         }
  906.         return 0;
  907.     }
  908.     /* Seek offset is within current buffer */
  909.     vfile("vpos=%ld", vpos);
  910.     return 0;
  911. }
  912. #define fseek fooseek
  913. #endif
  914.  
  915.  
  916. /* VARARGS1 */
  917. vfile(f, a, b, c)
  918. register char *f;
  919. {
  920.     if (Verbose > 2) {
  921.         fprintf(stderr, f, a, b, c);
  922.         fprintf(stderr, "\n");
  923.     }
  924. }
  925.  
  926.  
  927. alrm()
  928. {
  929.     longjmp(tohere, -1);
  930. }
  931.  
  932.  
  933. #ifndef vax11c
  934. /*
  935.  * readline(timeout) reads character(s) from file descriptor 0
  936.  * timeout is in tenths of seconds
  937.  */
  938. readline(timeout)
  939. {
  940.     register int c;
  941.     static char byt[1];
  942.  
  943.     fflush(stdout);
  944.     if (setjmp(tohere)) {
  945.         zperr("TIMEOUT");
  946.         return TIMEOUT;
  947.     }
  948.     c = timeout/10;
  949.     if (c<2)
  950.         c=2;
  951.     if (Verbose>5) {
  952.         fprintf(stderr, "Timeout=%d Calling alarm(%d) ", timeout, c);
  953.     }
  954.     signal(SIGALRM, alrm); alarm(c);
  955.     c=read(iofd, byt, 1);
  956.     alarm(0);
  957.     if (Verbose>5)
  958.         fprintf(stderr, "ret %x\n", byt[0]);
  959.     if (c<1)
  960.         return TIMEOUT;
  961.     return (byt[0]&0377);
  962. }
  963.  
  964. flushmo()
  965. {
  966.     fflush(stdout);
  967. }
  968.  
  969.  
  970. purgeline()
  971. {
  972. #ifdef USG
  973.     ioctl(iofd, TCFLSH, 0);
  974. #else
  975.     lseek(iofd, 0L, 2);
  976. #endif
  977. }
  978. #endif
  979.  
  980. /* send cancel string to get the other end to shut up */
  981. canit()
  982. {
  983.     static char canistr[] = {
  984.      24,24,24,24,24,24,24,24,24,24,8,8,8,8,8,8,8,8,8,8,0
  985.     };
  986.  
  987. #ifdef vax11c
  988.     raw_wbuf(strlen(canistr), canistr);
  989.     purgeline();
  990. #else
  991.     printf(canistr);
  992.     fflush(stdout);
  993. #endif
  994. }
  995.  
  996.  
  997. /*
  998.  * Log an error
  999.  */
  1000. /*VARARGS1*/
  1001. zperr(s,p,u)
  1002. char *s, *p, *u;
  1003. {
  1004.     if (Verbose <= 0)
  1005.         return;
  1006.     fprintf(stderr, "Retry %d: ", errors);
  1007.     fprintf(stderr, s, p, u);
  1008.     fprintf(stderr, "\n");
  1009. }
  1010.  
  1011. /*
  1012.  * substr(string, token) searches for token in string s
  1013.  * returns pointer to token within string if found, NULL otherwise
  1014.  */
  1015. char *
  1016. substr(s, t)
  1017. register char *s,*t;
  1018. {
  1019.     register char *ss,*tt;
  1020.     /* search for first char of token */
  1021.     for (ss=s; *s; s++)
  1022.         if (*s == *t)
  1023.             /* compare token with substring */
  1024.             for (ss=s,tt=t; ;) {
  1025.                 if (*tt == 0)
  1026.                     return s;
  1027.                 if (*ss++ != *tt++)
  1028.                     break;
  1029.             }
  1030.     return NULL;
  1031. }
  1032.  
  1033. char *babble[] = {
  1034. #ifdef vax11c
  1035.     "    Send file(s) with ZMODEM Protocol",
  1036.     "Usage:    sz [-2+abdefkLlNnquvwYy] [-] file ...",
  1037.     "    sz [-2Ceqv] -c COMMAND",
  1038.     "    \\ Force next option letter to upper case",
  1039. #else
  1040.     "Send file(s) with ZMODEM/YMODEM/XMODEM Protocol",
  1041.     "    (Y) = Option applies to YMODEM only",
  1042.     "    (Z) = Option applies to ZMODEM only",
  1043.     "Usage:    sz [-2+abdefkLlNnquvwYy] [-] file ...",
  1044.     "    sz [-2Ceqv] -c COMMAND",
  1045.     "    sb [-2adfkquv] [-] file ...",
  1046.     "    sx [-2akquv] [-] file",
  1047. #endif
  1048. #ifdef CSTOPB
  1049.     "    2 Use 2 stop bits",
  1050. #endif
  1051.     "    + Append to existing destination file (Z)",
  1052.     "    a (ASCII) change NL to CR/LF",
  1053.     "    b Binary file transfer override",
  1054.     "    c send COMMAND (Z)",
  1055. #ifndef vax11c
  1056.     "    d Change '.' to '/' in pathnames (Y/Z)",
  1057. #endif
  1058.     "    e Escape all control characters (Z)",
  1059.     "    f send Full pathname (Y/Z)",
  1060.     "    i send COMMAND, ack Immediately (Z)",
  1061.     "    k Send 1024 byte packets (Y)",
  1062.     "    L N Limit subpacket length to N bytes (Z)",
  1063.     "    l N Limit frame length to N bytes (l>=L) (Z)",
  1064.     "    n send file if source newer (Z)",
  1065.     "    N send file if source newer or longer (Z)",
  1066.     "    o Use 16 bit CRC instead of 32 bit CRC (Z)",
  1067.     "    p Protect existing destination file (Z)",
  1068.     "    r Resume/Recover interrupted file transfer (Z)",
  1069.     "    q Quiet (no progress reports)",
  1070. #ifndef vax11c
  1071.     "    u Unlink file after transmission",
  1072. #endif
  1073.     "    v Verbose - provide debugging information",
  1074.     "    w N Window is N bytes (Z)",
  1075.     "    Y Yes, overwrite existing file, skip if not present at rx (Z)",
  1076.     "    y Yes, overwrite existing file (Z)",
  1077.     "- as pathname sends standard input as sPID.sz or environment ONAME",
  1078.     ""
  1079. };
  1080.  
  1081. usage()
  1082. {
  1083.     char **pp;
  1084.  
  1085.     for (pp=babble; **pp; ++pp)
  1086.         fprintf(stderr, "%s\n", *pp);
  1087.     fprintf(stderr, "%s for %s by Chuck Forsberg, Omen Technology INC\n",
  1088.      VERSION, OS);
  1089.     fprintf(stderr, "\t\t\042The High Reliability Software\042\n");
  1090.     cucheck();
  1091.     exit(SS_NORMAL);
  1092. }
  1093.  
  1094. /*
  1095.  * Get the receiver's init parameters
  1096.  */
  1097. getzrxinit()
  1098. {
  1099.     register n;
  1100.     struct stat f;
  1101.  
  1102.     for (n=10; --n>=0; ) {
  1103.         
  1104.         switch (zgethdr(Rxhdr, 1)) {
  1105.         case ZCHALLENGE:    /* Echo receiver's challenge numbr */
  1106.             stohdr(Rxpos);
  1107.             zshhdr(ZACK, Txhdr);
  1108.             continue;
  1109.         case ZCOMMAND:        /* They didn't see out ZRQINIT */
  1110.             stohdr(0L);
  1111.             zshhdr(ZRQINIT, Txhdr);
  1112.             continue;
  1113.         case ZRINIT:
  1114.             Rxflags = 0377 & Rxhdr[ZF0];
  1115.             Txfcs32 = (Wantfcs32 && (Rxflags & CANFC32));
  1116.             Zctlesc |= Rxflags & TESCCTL;
  1117.             Rxbuflen = (0377 & Rxhdr[ZP0])+((0377 & Rxhdr[ZP1])<<8);
  1118.             if ( !(Rxflags & CANFDX))
  1119.                 Txwindow = 0;
  1120.             vfile("Rxbuflen=%d Tframlen=%d", Rxbuflen, Tframlen);
  1121.             if ( !Fromcu)
  1122.                 signal(SIGINT, SIG_IGN);
  1123. #ifdef MODE2OK
  1124.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  1125. #endif
  1126. #ifndef READCHECK
  1127. #ifndef USG
  1128.             /* Use 1024 byte frames if no sample/interrupt */
  1129.             if (Rxbuflen < 32 || Rxbuflen > 1024) {
  1130.                 Rxbuflen = 1024;
  1131.                 vfile("Rxbuflen=%d", Rxbuflen);
  1132.             }
  1133. #endif
  1134. #endif
  1135.             /* Override to force shorter frame length */
  1136.             if (Rxbuflen && (Rxbuflen>Tframlen) && (Tframlen>=32))
  1137.                 Rxbuflen = Tframlen;
  1138.             if ( !Rxbuflen && (Tframlen>=32) && (Tframlen<=1024))
  1139.                 Rxbuflen = Tframlen;
  1140.             vfile("Rxbuflen=%d", Rxbuflen);
  1141.  
  1142. #ifndef vax11c
  1143.             /* If using a pipe for testing set lower buf len */
  1144.             fstat(iofd, &f);
  1145.             if ((f.st_mode & S_IFMT) != S_IFCHR) {
  1146.                 Rxbuflen = 1024;
  1147.             }
  1148. #endif
  1149. #ifdef BADSEEK
  1150.             Canseek = 0;
  1151.             Txwindow = TXBSIZE - 1024;
  1152.             Txwspac = TXBSIZE/4;
  1153. #endif
  1154.             /*
  1155.              * If input is not a regular file, force ACK's to
  1156.              *  prevent running beyond the buffer limits
  1157.              */
  1158.             if ( !Command) {
  1159.                 fstat(fileno(in), &f);
  1160.                 if ((f.st_mode & S_IFMT) != S_IFREG) {
  1161.                     Canseek = -1;
  1162. #ifdef TXBSIZE
  1163.                     Txwindow = TXBSIZE - 1024;
  1164.                     Txwspac = TXBSIZE/4;
  1165. #else
  1166.                     Rxbuflen = 1024;
  1167. #endif
  1168.                 }
  1169.             }
  1170.             /* Set initial subpacket length */
  1171.             if (blklen < 1024) {    /* Command line override? */
  1172.                 if (Baudrate > 300)
  1173.                     blklen = 256;
  1174.                 if (Baudrate > 1200)
  1175.                     blklen = 512;
  1176.                 if (Baudrate > 2400)
  1177.                     blklen = 1024;
  1178.             }
  1179.             if (Rxbuflen && blklen>Rxbuflen)
  1180.                 blklen = Rxbuflen;
  1181.             if (blkopt && blklen > blkopt)
  1182.                 blklen = blkopt;
  1183.             vfile("Rxbuflen=%d blklen=%d", Rxbuflen, blklen);
  1184.             vfile("Txwindow = %u Txwspac = %d", Txwindow, Txwspac);
  1185.  
  1186.             return (sendzsinit());
  1187.         case ZCAN:
  1188.         case TIMEOUT:
  1189.             return ERROR;
  1190.         case ZRQINIT:
  1191.             if (Rxhdr[ZF0] == ZCOMMAND)
  1192.                 continue;
  1193.         default:
  1194.             zshhdr(ZNAK, Txhdr);
  1195.             continue;
  1196.         }
  1197.     }
  1198.     return ERROR;
  1199. }
  1200.  
  1201. /* Send send-init information */
  1202. sendzsinit()
  1203. {
  1204.     register c;
  1205.  
  1206.     if (Myattn[0] == '\0' && (!Zctlesc || (Rxflags & TESCCTL)))
  1207.         return OK;
  1208.     errors = 0;
  1209.     for (;;) {
  1210.         stohdr(0L);
  1211.         if (Zctlesc) {
  1212.             Txhdr[ZF0] |= TESCCTL; zshhdr(ZSINIT, Txhdr);
  1213.         }
  1214.         else
  1215.             zsbhdr(ZSINIT, Txhdr);
  1216.         zsdata(Myattn, 1+strlen(Myattn), ZCRCW);
  1217.         c = zgethdr(Rxhdr, 1);
  1218.         switch (c) {
  1219.         case ZCAN:
  1220.             return ERROR;
  1221.         case ZACK:
  1222.             return OK;
  1223.         default:
  1224.             if (++errors > 19)
  1225.                 return ERROR;
  1226.             continue;
  1227.         }
  1228.     }
  1229. }
  1230.  
  1231. /* Send file name and related info */
  1232. zsendfile(buf, blen)
  1233. char *buf;
  1234. {
  1235.     register c;
  1236.  
  1237.     for (;;) {
  1238.         Txhdr[ZF0] = Lzconv;    /* file conversion request */
  1239.         Txhdr[ZF1] = Lzmanag;    /* file management request */
  1240.         if (Lskipnocor)
  1241.             Txhdr[ZF1] |= ZMSKNOLOC;
  1242.         Txhdr[ZF2] = Lztrans;    /* file transport request */
  1243.         Txhdr[ZF3] = 0;
  1244.         zsbhdr(ZFILE, Txhdr);
  1245.         zsdata(buf, blen, ZCRCW);
  1246. again:
  1247.         c = zgethdr(Rxhdr, 1);
  1248.         switch (c) {
  1249.         case ZRINIT:
  1250.             while ((c = readline(50)) > 0)
  1251.                 if (c == ZPAD) {
  1252.                     goto again;
  1253.                 }
  1254.             /* **** FALL THRU TO **** */
  1255.         default:
  1256.             continue;
  1257.         case ZCAN:
  1258.         case TIMEOUT:
  1259.         case ZABORT:
  1260.         case ZFIN:
  1261.             return ERROR;
  1262.         case ZSKIP:
  1263.             fclose(in); return c;
  1264.         case ZRPOS:
  1265.             /*
  1266.              * Suppress zcrcw request otherwise triggered by
  1267.              * lastyunc==bytcnt
  1268.              */
  1269.             if (Rxpos && fseek(in, Rxpos, 0))
  1270.                 return ERROR;
  1271.             Lastsync = (bytcnt = Txpos = Rxpos) -1;
  1272.             Dontread = FALSE;
  1273.             return zsendfdata();
  1274.         }
  1275.     }
  1276. }
  1277.  
  1278. /* Send the data in the file */
  1279. zsendfdata()
  1280. {
  1281.     register c, e, n;
  1282.     register newcnt;
  1283.     register long tcount = 0;
  1284.     int junkcount;        /* Counts garbage chars received by TX */
  1285.     static int tleft = 6;    /* Counter for test mode */
  1286.  
  1287.     Lrxpos = 0;
  1288.     junkcount = 0;
  1289.     Beenhereb4 = FALSE;
  1290. somemore:
  1291.     if (setjmp(intrjmp)) {
  1292. waitack:
  1293.         junkcount = 0;
  1294.         c = getinsync(0);
  1295. gotack:
  1296.         switch (c) {
  1297.         default:
  1298.         case ZCAN:
  1299.             fclose(in);
  1300.             return ERROR;
  1301.         case ZSKIP:
  1302.             fclose(in);
  1303.             return c;
  1304.         case ZACK:
  1305.         case ZRPOS:
  1306.             break;
  1307.         case ZRINIT:
  1308.             return OK;
  1309.         }
  1310. #ifdef READCHECK
  1311.         /*
  1312.          * If the reverse channel can be tested for data,
  1313.          *  this logic may be used to detect error packets
  1314.          *  sent by the receiver, in place of setjmp/longjmp
  1315.          *  rdchk(fdes) returns non 0 if a character is available
  1316.          */
  1317.         while (rdchk(iofd)) {
  1318. #ifdef SV
  1319.             switch (checked)
  1320. #else
  1321.             switch (readline(1))
  1322. #endif
  1323.             {
  1324.             case CAN:
  1325.             case ZPAD:
  1326.                 c = getinsync(1);
  1327.                 goto gotack;
  1328.             case XOFF:        /* Wait a while for an XON */
  1329.             case XOFF|0200:
  1330.                 readline(100);
  1331.             }
  1332.         }
  1333. #endif
  1334.     }
  1335.  
  1336.     if ( !Fromcu)
  1337.         signal(SIGINT, onintr);
  1338.     newcnt = Rxbuflen;
  1339.     Txwcnt = 0;
  1340.     stohdr(Txpos);
  1341.     zsbhdr(ZDATA, Txhdr);
  1342.  
  1343.     /*
  1344.      * Special testing mode.  This should force receiver to Attn,ZRPOS
  1345.      *  many times.  Each time the signal should be caught, causing the
  1346.      *  file to be started over from the beginning.
  1347.      */
  1348.     if (Test) {
  1349.         if ( --tleft)
  1350.             while (tcount < 20000) {
  1351.                 printf(qbf); fflush(stdout);
  1352.                 tcount += strlen(qbf);
  1353. #ifdef READCHECK
  1354.                 while (rdchk(iofd)) {
  1355. #ifdef SV
  1356.                     switch (checked)
  1357. #else
  1358.                     switch (readline(1))
  1359. #endif
  1360.                     {
  1361.                     case CAN:
  1362.                     case ZPAD:
  1363. #ifdef TCFLSH
  1364.                         ioctl(iofd, TCFLSH, 1);
  1365. #endif
  1366.                         goto waitack;
  1367.                     case XOFF:    /* Wait for XON */
  1368.                     case XOFF|0200:
  1369.                         readline(100);
  1370.                     }
  1371.                 }
  1372. #endif
  1373.             }
  1374.         signal(SIGINT, SIG_IGN); canit();
  1375.         sleep(3); purgeline(); mode(0);
  1376.         printf("\nsz: Tcount = %ld\n", tcount);
  1377.         if (tleft) {
  1378.             printf("ERROR: Interrupts Not Caught\n");
  1379.             exit(1);
  1380.         }
  1381.         exit(SS_NORMAL);
  1382.     }
  1383.  
  1384.     do {
  1385.         if (Dontread) {
  1386.             n = Lastn;
  1387.         } else {
  1388.             n = zfilbuf();
  1389.             Lastread = Txpos;  Lastn = n;
  1390.         }
  1391.         Dontread = FALSE;
  1392.         if (Eofseen)
  1393.             e = ZCRCE;
  1394.         else if (junkcount > 3)
  1395.             e = ZCRCW;
  1396.         else if (bytcnt == Lastsync)
  1397.             e = ZCRCW;
  1398.         else if (Rxbuflen && (newcnt -= n) <= 0)
  1399.             e = ZCRCW;
  1400.         else if (Txwindow && (Txwcnt += n) >= Txwspac) {
  1401.             Txwcnt = 0;  e = ZCRCQ;
  1402.         }
  1403.         else
  1404.             e = ZCRCG;
  1405.         if (Verbose>1)
  1406.             fprintf(stderr, "\r%7ld ZMODEM%s    ",
  1407.               Txpos, Crc32t?" CRC-32":"");
  1408.         zsdata(txbuf, n, e);
  1409.         bytcnt = Txpos += n;
  1410.         if (e == ZCRCW)
  1411.             goto waitack;
  1412. #ifdef READCHECK
  1413.         /*
  1414.          * If the reverse channel can be tested for data,
  1415.          *  this logic may be used to detect error packets
  1416.          *  sent by the receiver, in place of setjmp/longjmp
  1417.          *  rdchk(fdes) returns non 0 if a character is available
  1418.          */
  1419.         fflush(stdout);
  1420.         while (rdchk(iofd)) {
  1421. #ifdef SV
  1422.             switch (checked)
  1423. #else
  1424.             switch (readline(1))
  1425. #endif
  1426.             {
  1427.             case CAN:
  1428.             case ZPAD:
  1429.                 c = getinsync(1);
  1430.                 if (c == ZACK)
  1431.                     break;
  1432. #ifdef TCFLSH
  1433.                 ioctl(iofd, TCFLSH, 1);
  1434. #endif
  1435.                 /* zcrce - dinna wanna starta ping-pong game */
  1436.                 zsdata(txbuf, 0, ZCRCE);
  1437.                 goto gotack;
  1438.             case XOFF:        /* Wait a while for an XON */
  1439.             case XOFF|0200:
  1440.                 readline(100);
  1441.             default:
  1442.                 ++junkcount;
  1443.             }
  1444.         }
  1445. #endif    /* READCHECK */
  1446.         if (Txwindow) {
  1447.             while ((tcount = Txpos - Lrxpos) >= Txwindow) {
  1448.                 vfile("%ld window >= %u", tcount, Txwindow);
  1449.                 if (e != ZCRCQ)
  1450.                     zsdata(txbuf, 0, e = ZCRCQ);
  1451.                 c = getinsync(1);
  1452.                 if (c != ZACK) {
  1453. #ifdef TCFLSH
  1454.                     ioctl(iofd, TCFLSH, 1);
  1455. #endif
  1456.                     zsdata(txbuf, 0, ZCRCE);
  1457.                     goto gotack;
  1458.                 }
  1459.             }
  1460.             vfile("window = %ld", tcount);
  1461.         }
  1462.     } while (!Eofseen);
  1463.     if ( !Fromcu)
  1464.         signal(SIGINT, SIG_IGN);
  1465.  
  1466.     for (;;) {
  1467.         stohdr(Txpos);
  1468.         zsbhdr(ZEOF, Txhdr);
  1469.         switch (getinsync(0)) {
  1470.         case ZACK:
  1471.             continue;
  1472.         case ZRPOS:
  1473.             goto somemore;
  1474.         case ZRINIT:
  1475.             return OK;
  1476.         case ZSKIP:
  1477.             fclose(in);
  1478.             return c;
  1479.         default:
  1480.             fclose(in);
  1481.             return ERROR;
  1482.         }
  1483.     }
  1484. }
  1485.  
  1486. /*
  1487.  * Respond to receiver's complaint, get back in sync with receiver
  1488.  */
  1489. getinsync(flag)
  1490. {
  1491.     register c;
  1492.  
  1493.     for (;;) {
  1494.         if (Test) {
  1495.             printf("\r\n\n\n***** Signal Caught *****\r\n");
  1496.             Rxpos = 0; c = ZRPOS;
  1497.         } else
  1498.             c = zgethdr(Rxhdr, 0);
  1499.         switch (c) {
  1500.         case ZCAN:
  1501.         case ZABORT:
  1502.         case ZFIN:
  1503.         case TIMEOUT:
  1504.             return ERROR;
  1505.         case ZRPOS:
  1506.             /* ************************************* */
  1507.             /*  If sending to a buffered modem, you  */
  1508.             /*   might send a break at this point to */
  1509.             /*   dump the modem's buffer.         */
  1510.             if (Lastn >= 0 && Lastread == Rxpos) {
  1511.                 Dontread = TRUE;
  1512.             } else {
  1513.                 clearerr(in);    /* In case file EOF seen */
  1514.                 if (fseek(in, Rxpos, 0))
  1515.                     return ERROR;
  1516.                 Eofseen = 0;
  1517.             }
  1518.             bytcnt = Lrxpos = Txpos = Rxpos;
  1519.             if (Lastsync == Rxpos) {
  1520.                 if (++Beenhereb4 > 4)
  1521.                     if (blklen > 32)
  1522.                         blklen /= 2;
  1523.             }
  1524.             Lastsync = Rxpos;
  1525.             return c;
  1526.         case ZACK:
  1527.             Lrxpos = Rxpos;
  1528.             if (flag || Txpos == Rxpos)
  1529.                 return ZACK;
  1530.             continue;
  1531.         case ZRINIT:
  1532.         case ZSKIP:
  1533.             fclose(in);
  1534.             return c;
  1535.         case ERROR:
  1536.         default:
  1537.             zsbhdr(ZNAK, Txhdr);
  1538.             continue;
  1539.         }
  1540.     }
  1541. }
  1542.  
  1543.  
  1544. /* Say "bibi" to the receiver, try to do it cleanly */
  1545. saybibi()
  1546. {
  1547.     for (;;) {
  1548.         stohdr(0L);        /* CAF Was zsbhdr - minor change */
  1549.         zshhdr(ZFIN, Txhdr);    /*  to make debugging easier */
  1550.         switch (zgethdr(Rxhdr, 0)) {
  1551.         case ZFIN:
  1552.             sendline('O'); sendline('O'); flushmo();
  1553.         case ZCAN:
  1554.         case TIMEOUT:
  1555.             return;
  1556.         }
  1557.     }
  1558. }
  1559.  
  1560. /* Local screen character display function */
  1561. bttyout(c)
  1562. {
  1563.     if (Verbose)
  1564.         putc(c, stderr);
  1565. }
  1566.  
  1567. /* Send command and related info */
  1568. zsendcmd(buf, blen)
  1569. char *buf;
  1570. {
  1571.     register c;
  1572.     long cmdnum;
  1573.  
  1574.     cmdnum = getpid();
  1575.     errors = 0;
  1576.     for (;;) {
  1577.         stohdr(cmdnum);
  1578.         Txhdr[ZF0] = Cmdack1;
  1579.         zsbhdr(ZCOMMAND, Txhdr);
  1580.         zsdata(buf, blen, ZCRCW);
  1581. listen:
  1582.         Rxtimeout = 100;        /* Ten second wait for resp. */
  1583.         c = zgethdr(Rxhdr, 1);
  1584.  
  1585.         switch (c) {
  1586.         case ZRINIT:
  1587.             goto listen;    /* CAF 8-21-87 */
  1588.         case ERROR:
  1589.         case TIMEOUT:
  1590.             if (++errors > Cmdtries)
  1591.                 return ERROR;
  1592.             continue;
  1593.         case ZCAN:
  1594.         case ZABORT:
  1595.         case ZFIN:
  1596.         case ZSKIP:
  1597.         case ZRPOS:
  1598.             return ERROR;
  1599.         default:
  1600.             if (++errors > 20)
  1601.                 return ERROR;
  1602.             continue;
  1603.         case ZCOMPL:
  1604.             Exitcode = Rxpos;
  1605.             saybibi();
  1606.             return OK;
  1607.         case ZRQINIT:
  1608. #ifdef vax11c        /* YAMP :== Yet Another Missing Primitive */
  1609.             return ERROR;
  1610. #else
  1611.             vfile("******** RZ *******");
  1612.             system("rz");
  1613.             vfile("******** SZ *******");
  1614.             goto listen;
  1615. #endif
  1616.         }
  1617.     }
  1618. }
  1619.  
  1620. /*
  1621.  * If called as sb use YMODEM protocol
  1622.  */
  1623. chkinvok(s)
  1624. char *s;
  1625. {
  1626. #ifdef vax11c
  1627.     Progname = "sz";
  1628. #else
  1629.     register char *p;
  1630.  
  1631.     p = s;
  1632.     while (*p == '-')
  1633.         s = ++p;
  1634.     while (*p)
  1635.         if (*p++ == '/')
  1636.             s = p;
  1637.     if (*s == 'v') {
  1638.         Verbose=1; ++s;
  1639.     }
  1640.     Progname = s;
  1641.     if (s[0]=='s' && s[1]=='b') {
  1642.         Nozmodem = TRUE; blklen=1024;
  1643.     }
  1644.     if (s[0]=='s' && s[1]=='x') {
  1645.         Modem2 = TRUE;
  1646.     }
  1647. #endif
  1648. }
  1649.  
  1650. countem(argc, argv)
  1651. register char **argv;
  1652. {
  1653.     register c;
  1654.     struct stat f;
  1655.  
  1656.     for (Totalleft = 0, Filesleft = 0; --argc >=0; ++argv) {
  1657.         f.st_size = -1;
  1658.         if (Verbose>2) {
  1659.             fprintf(stderr, "\nCountem: %03d %s ", argc, *argv);
  1660.             fflush(stderr);
  1661.         }
  1662.         if (access(*argv, 04) >= 0 && stat(*argv, &f) >= 0) {
  1663.             c = f.st_mode & S_IFMT;
  1664.             if (c != S_IFDIR && c != S_IFBLK) {
  1665.                 ++Filesleft;  Totalleft += f.st_size;
  1666.             }
  1667.         }
  1668.         if (Verbose>2)
  1669.             fprintf(stderr, " %ld", f.st_size);
  1670.     }
  1671.     if (Verbose>2)
  1672.         fprintf(stderr, "\ncountem: Total %d %ld\n",
  1673.           Filesleft, Totalleft);
  1674. }
  1675.  
  1676. chartest(m)
  1677. {
  1678.     register n;
  1679.  
  1680.     mode(m);
  1681.     printf("\r\n\nCharacter Transparency Test Mode %d\r\n", m);
  1682.     printf("If Pro-YAM/ZCOMM is not displaying ^M hit ALT-V NOW.\r\n");
  1683.     printf("Hit Enter.\021");  fflush(stdout);
  1684.     readline(500);
  1685.  
  1686.     for (n = 0; n < 256; ++n) {
  1687.         if (!(n%8))
  1688.             printf("\r\n");
  1689.         printf("%02x ", n);  fflush(stdout);
  1690.         sendline(n);    flushmo();
  1691.         printf("  ");  fflush(stdout);
  1692.         if (n == 127) {
  1693.             printf("Hit Enter.\021");  fflush(stdout);
  1694.             readline(500);
  1695.             printf("\r\n");  fflush(stdout);
  1696.         }
  1697.     }
  1698.     printf("\021\r\nEnter Characters, echo is in hex.\r\n");
  1699.     printf("Hit SPACE or pause 40 seconds for exit.\r\n");
  1700.  
  1701.     while (n != TIMEOUT && n != ' ') {
  1702.         n = readline(400);
  1703.         printf("%02x\r\n", n);
  1704.         fflush(stdout);
  1705.     }
  1706.     printf("\r\nMode %d character transparency test ends.\r\n", m);
  1707.     fflush(stdout);
  1708. }
  1709.  
  1710. /* End of sz.c */
  1711.